<template>
  <div class="main-layout">
    <div class="background-layer"></div>

    <div class="content-wrapper">
      <header class="header">
        <div class="container">
          <div class="logo">
            <router-link to="/">宠物购买网站</router-link>
          </div>
          <nav class="nav">
            <router-link to="/">首页</router-link>
            <template v-if="!userStore.isLoggedIn">
              <router-link to="/login">登录</router-link>
              <router-link to="/register">注册</router-link>
            </template>
            <template v-else>
              <router-link to="/user/my-orders">我的申请</router-link>
              <router-link to="/user/profile">个人中心</router-link>
              <router-link to="/admin" v-if="userStore.isAdmin">后台管理</router-link>
              <a href="javascript:;" @click="handleLogout">退出</a>
              <div class="user-avatar" @click="router.push('/user/profile')">
                <el-avatar :size="32" :src="userStore.userInfo?.userAvatar || 'https://via.placeholder.com/32'"></el-avatar>
              </div>
            </template>
          </nav>
        </div>
      </header>

      <main class="main-content">
        <router-view />
      </main>

      <footer class="footer">
        <div class="container">
          <p>线上宠物商城</p>
        </div>
      </footer>
    </div>
  </div>
</template>

<script setup>
// ... script部分无需改动 ...
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/userStore'
import { ElAvatar } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const handleLogout = async () => {
  await userStore.logout()
  router.push('/login')
}

onMounted(() => {
  if (userStore.isLoggedIn) {
    userStore.fetchCurrentUser()
  }
})
</script>

<style scoped>
.main-layout {
  position: relative;
  min-height: 100vh;
}
.background-layer {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: -1;
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  animation: cycleBackground 20s infinite;
}
.content-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: transparent;
}
.header {
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 1000;
}
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.logo a {
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
  text-decoration: none;
}
.nav {
  display: flex;
  gap: 20px;
  align-items: center;
}
.nav a {
  color: #333;
  text-decoration: none;
  transition: color 0.3s;
}
.nav a:hover, .nav a.router-link-active {
  color: #409eff;
}
.user-avatar {
  cursor: pointer;
  margin-left: 10px;
  transition: transform 0.2s;
}
.user-avatar:hover {
  transform: scale(1.1);
}
.main-content {
  flex: 1;
}
.footer {
  background-color: #fff;
  padding: 20px 0;
  text-align: center;
  color: #909399;
  border-top: 1px solid #e4e7ed;
}
</style>